<?php


namespace App\Shuixing\Controllers;


use App\Lib\WeApp\WeApp;
use App\Wen\Utils\FieldUtils;
use App\Http\Controllers\Api\BaseController;
use App\Lib\JwtAuth;
use App\Models\Posts\WxLike;
use App\Models\WxCommentLike;
use App\Models\User\WxUser;
use App\Models\User\WxUserFollow;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Storage;

class LoginController extends BaseController
{
    public function index()
    {
        global $__MINI_GLOBAL_IS_ADMIN_SUPER__;
        $__MINI_GLOBAL_IS_ADMIN_SUPER__ = false;

        //例如，在laravel,文件缓存路径为storage_path('cache/')
        $weapp = new WeApp();
        //从‘小程序’获取一个‘二维码’单例对象
        $qrcode = $weapp->getQRCode();
        $scene = uniqid();
//        $page = 'pages/login/login';
        $page = 'pages/pc-login/index';
        //2.获取小程序B码
        $qrcodeContent = $qrcode->getQRCodeB($scene, $page, 430, false, $line_color = null);
        $filePath = 'qrcode/' . $scene . '.png';
        $res = Storage::disk('public')->put($filePath, $qrcodeContent);
        if ($res) {
            $qrcodeurl = Storage::url($filePath);
        }
        return view('shuixing/login', compact('qrcodeurl', 'scene'));
    }

    public function pcUserinfo(Request $request)
    {
        $token = $request->header('token');
        if ($token) {
            $uid = JwtAuth::getUserId($token, 'uid');
            if($uid > 0){
                $data = WxUser::where('id', $uid)
                    ->first(FieldUtils::userInfoColums())
                    ->toArray();

                $data['followTotal'] = WxUserFollow::where('user_id', $uid)->count();
                $data['fansTotal'] = WxUserFollow::where('user_follow_id', $uid)->count();
                $count1 = WxLike::where('posts_user_id', $uid)->count();
                $count2 = WxCommentLike::where('comment_user_id', $uid)->count();
                $data['likeTotal'] = $count1 + $count2;
                $data['token'] = $token;
                return $data;
            }
        }else{
            return null;
        }
    }
}
